﻿<template>
  <div>
    <el-dialog v-drag :title='dialogTitle' :visible.sync='dialogFormVisible' width='60%' @close="closeDialog">
      <el-form :model="ruleSubCloumnsForm" ref="ruleSubCloumnsForm" :rules='rulesSub'>
        <div class='page_tableContent'>
          <el-table :data='ruleSubCloumnsForm.subColumnsTable' style='width:100%' empty-text='暂无栏位信息，请立即添加' border stripe class='editTable'>
            <el-table-column align='center' label='栏位名称' :render-header='handelHelp' label-class-name="从选定的从表总选择栏位，栏位名称不能为空">
              <template slot-scope='scope'>
                <el-form-item :prop="'subColumnsTable[' + scope.$index + '].column_name'" :rules='rulesSub.column_name'>
                  <span v-if='!isEdit'>{{scope.row.column_name}}</span>
                  <el-select v-else v-model="scope.row.column_name" placeholder="请选择"
                  @focus='subColumnSelect(scope.row)' @change='subColumnSelectChange(scope.row, scope.$index)' filterable>
                    <el-option v-for='(item, index) in selectColumnsList' :key='index'
                    :label="item.column_desc ? item.column_name + '(' + item.column_desc + ')' : item.column_name" :value="item.column_name"></el-option>
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column align='center' label='表头名称' :render-header='handelHelp' label-class-name="生成的表格时该栏位的表头名称">
              <template slot-scope='scope'>
                <el-form-item>
                  <el-input :disabled='!isEdit' v-model="scope.row.column_desc" placeholder="请输入"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column align='center' label='最大长度' :render-header='handelHelp' label-class-name="允许该字段输入的最大长度，为空或负数时则不做限定">
              <template slot-scope='scope'>
                <el-form-item>
                  <el-input :disabled='!isEdit' v-model="scope.row.max_length" placeholder="请输入"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column align='center' label='标签提示' :render-header='handelHelp' label-class-name="标签提示说明">
              <template slot-scope='scope'>
                <el-form-item>
                  <el-input :disabled='!isEdit' v-model="scope.row.tips" placeholder="请输入"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column align='center' width='80' label='是否必输' :render-header='handelHelp' label-class-name="表示该栏位是否为必输的">
              <template slot-scope='scope'>
                <el-form-item>
                  <el-switch :disabled='!isEdit' v-model="scope.row.check_required"></el-switch>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column align='center' label='正则检查' :render-header='handelHelp' label-class-name="对该栏位输入规则的正则表达式检查">
              <template slot-scope='scope'>
                <el-form-item>
                  <el-input :disabled='!isEdit' v-model="scope.row.check_regex" placeholder="请输入"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column align='center' width='80' label='排序编号' :render-header='handelHelp' label-class-name="该栏位在表格中展示的顺序">
              <template slot-scope='scope'>
                <el-form-item>
                  <el-input :disabled='!isEdit' v-model="scope.row.sort_num" placeholder="请输入"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column align='center' width='100' label='备注' :render-header='handelHelp' label-class-name="该栏位的备注说明">
              <template slot-scope='scope'>
                <el-form-item>
                  <el-input :disabled='!isEdit' v-model="scope.row.memo" placeholder="请输入"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column align="center" label='操作' width='70'>
              <template slot-scope="scope">
                <el-button :disabled='!isEdit' type='text' @click="handleDeleteSubCloumn(scope.$index)"><i class='iconfont icon-shanchu'></i>删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 底部按钮和页码部分 -->
        <div class='page_bottomContent' v-if = 'isEdit'>
          <div class='page_btnsContent'>
            <el-form-item class='form_noLeftMargin'>
              <el-button type="primary" @click='addSubColumns ()'>添加栏位信息</el-button>
              <el-button type='primary' @click="resetSearchForm ('ruleSubCloumnsForm')">取 消</el-button>
              <el-button type="primary" @click="submitSearchForm ('ruleSubCloumnsForm')">确 定</el-button>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ['showSubCloumsDialog', 'subCloumsList'],
  data () {
    return {
      dialogTitle: '',
      dialogFormVisible: false,
      // 可选的栏位列表
      selectColumnsList: [],
      // 查看形式
      isEdit: false,
      // 表单的信息
      ruleSubCloumnsForm: {
        subColumnsTable: [
          {
            column_name: '',
            column_desc: '',
            max_length: '',
            tips: '',
            check_required: false,
            check_regex: '',
            sort_num: 0,
            memo: ''
          }
        ]
      },
      rulesSub: {
        column_name: [
          {required: true, message: '栏位名称不能为空', trigger: 'change'}
        ]
      }
    }
  },
  methods: {
    // 表头的提示信息
    handelHelp (h, {column}) {
      if (column.labelClassName) {
        return (
          <el-tooltip effect="dark" placement="top">
            <div slot='content'>{column.labelClassName}</div>
            <span>{column.label}</span>
          </el-tooltip>
        )
      }
    },
    // 初始化表格信息
    initTable () {
      this.ruleSubCloumnsForm.subColumnsTable = this.subCloumsList.columns
      if (this.subCloumsList.showType === 'edit') {
        this.dialogTitle = '编辑' + this.subCloumsList.myMetaName + '栏位信息'
        this.isEdit = true
      } else {
        this.dialogTitle = '查看' + this.subCloumsList.myMetaName + '栏位信息'
        this.isEdit = false
      }
    },
    // 新增栏位信息
    addSubColumns () {
      this.ruleSubCloumnsForm.subColumnsTable.push({
        column_name: '',
        column_desc: '',
        max_length: '',
        tips: '',
        check_required: false,
        check_regex: '',
        sort_num: 0,
        memo: ''
      })
    },
    // 删除栏位信息
    handleDeleteSubCloumn (index) {
      this.$confirm('此操作将永久删除该栏位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'el-button--info'
      }).then(() => {
        this.ruleSubCloumnsForm.subColumnsTable.splice(index, 1)
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      }).catch(() => {})
    },
    // 选择主字段时
    subColumnSelect (row) {
      this.selectColumnsList = []
      this.subCloumsList.totalColumns.forEach(totalVal => {
        let surePush = true
        this.ruleSubCloumnsForm.subColumnsTable.forEach(val => {
          if (val.column_name === totalVal.column_name && row.column_name !== val.column_name) {
            surePush = false
          }
        })
        if (surePush) {
          this.selectColumnsList.push(totalVal)
        }
      })
    },
    // 选择栏位字段改变时
    subColumnSelectChange (row, rowIndex) {
      this.selectColumnsList.forEach(val => {
        if (row.column_name === val.column_name) {
          this.ruleSubCloumnsForm.subColumnsTable[rowIndex].column_desc = val.column_desc
        }
      })
    },
    // 点击取消按钮时
    resetSearchForm (forms) {
      this.$refs[forms].clearValidate()
      this.$emit('giveUpSubCloumns')
    },
    // 点击上部的X关闭弹窗时
    closeDialog () {
      this.$emit('giveUpSubCloumns')
      this.$refs.ruleSubCloumnsForm.clearValidate()
    },
    // 点击确定提交的按钮时
    submitSearchForm (forms) {
      this.$refs[forms].validate((valid) => {
        if (valid) {
          let toParent = {
            index: this.subCloumsList.index,
            columns: this.ruleSubCloumnsForm.subColumnsTable
          }
          this.$emit('successSubCloumns', toParent)
        } else {
          return false
        }
      })
    }
  },
  watch: {
    showSubCloumsDialog: {
      handler: function (val, oldVal) {
        this.dialogFormVisible = val
        if (val) {
          this.initTable()
        }
      },
      deep: true
    }
  }
}
</script>
